package com.api.controller

import com.base.controller.call
import com.base.db.mysql.MySQLService
import com.base.db.redis.RedisService
import com.base.rpc.IRPC_API
import com.base.third.mob.MobService
import com.google.code.kaptcha.impl.DefaultKaptcha
import com.yfree.global.YController
import io.seata.spring.annotation.GlobalTransactional
import org.springframework.cloud.context.config.annotation.RefreshScope
import org.springframework.web.bind.annotation.RestController
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@RefreshScope
@RestController
class Controller_API(
        val mysqlService: MySQLService,
        val redisService: RedisService,
        val defaultKaptcha: DefaultKaptcha,
        val mobService: MobService,
) : YController(), IRPC_API {
    @GlobalTransactional
    override fun index() = yResult()

    @GlobalTransactional
    override fun code(request: HttpServletRequest, response: HttpServletResponse) = doCode(request, response)

    @GlobalTransactional
    override fun `interface`(jkid: String, jkparam: Any?) =
            when {
                jkid.startsWith("APP_") -> call(Controller_API_App.instance, jkid, jkparam)
                jkid.startsWith("SERVICE_") -> call(Controller_API_Service.instance, jkid, jkparam)
                jkid.startsWith("ADMIN_") -> call(Controller_API_Admin.instance, jkid, jkparam)
                else -> null
            } ?: yResult(code = 404, message = "jkid不存在")
}
